using System;

namespace MyUtil
{
    public class Timer{
        float waitTime,
            passTime;
        bool oneShot,
            shotted,
            pause;
        Action callBack;
        
        public float PassTime {  get { return passTime; } set { passTime = value; } }

        public Timer(float waitTime,Action callBack,bool oneShot=false)
        {
            this.waitTime=waitTime;
            passTime=0;
            pause=true;
            shotted=false;
            this.callBack=callBack;
            this.oneShot=oneShot;
        }

        public void Update(float deltaTime)
        {
            if(pause||(oneShot&&shotted))
                return;
            passTime+=deltaTime;
            if(passTime>=waitTime)
            {
                if(!oneShot||(oneShot&&!shotted))
                    callBack();
                passTime=0;
                shotted=true;
            }
        }
        
        public void Pause() => pause=true;
        public void Resume() => pause=false;
        public void Start()
        {
            passTime=0;
            pause=false;
            shotted=false;
        }
    }
}